package com.web.controller.pay;

/**
 * Created by Administrator on 2017/3/6.
 */

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 返回
 *
 * @author ekoo
 */
public class PayResponse<T> implements Serializable {
    private int status;/**1成功，非1失败**/
    private String error;//错误类型
    private String message;//错误具体内容
    protected  T params;
    private long timestamp;
    public boolean isOk(){
        if(status==1)
            return true;
        else return false;

    }

    public PayResponse() {
        this.timestamp=System.currentTimeMillis();
    }

    public PayResponse failure() {
        this.status=0;
        this.error="1";
        this.message="失败!";
        this.timestamp=System.currentTimeMillis();
        return this;
    }
    public PayResponse failure(String error, String message) {
        this.status=0;
        this.error=error;
        this.message=message;
        this.timestamp=System.currentTimeMillis();
        return this;
    }
    public PayResponse failure(String message) {
        this.status=0;
        this.error="1";
        this.message=message;
        this.timestamp=System.currentTimeMillis();
        return this;
    }

    public PayResponse failure(int status, String message) {
        this.status=status;
        this.error="1";
        this.message=message;
        this.timestamp=System.currentTimeMillis();
        return this;
    }

    public PayResponse ok() {
        this.status=1;
        this.error="0";
        this.message="成功!";
        this.timestamp=System.currentTimeMillis();
        return this;
    }
    public PayResponse ok(String message) {
        this.status=1;
        this.error="0";
        this.message=message;
        this.timestamp=System.currentTimeMillis();
        return this;
    }
    public static PayResponse custom(){
        PayResponse response=new PayResponse();
        return response;
    }
    public static PayResponse List(){
        PayResponse<List> response=new PayResponse<List>();
        if(response.getParams()==null){
            response.setParams(Lists.newArrayList());
        }
        return response;
    }
    public static PayResponse Map(){
        PayResponse<Map> response=new PayResponse<Map>();
        if(response.getParams()==null){
            response.setParams(Maps.newHashMap());
        }
        return response;
    }
    public static PayResponse Map(String key, Object value){
        PayResponse<Map> response=new PayResponse<Map>();
        if(response.getParams()==null){
            response.setParams(Maps.newHashMap());
            response.getParams().put(key,value);
        }
        return response;
    }
    public static PayResponse Map(Map map){
        PayResponse<Map> response=new PayResponse<Map>();
        if(response.getParams()==null){
            response.setParams(Maps.newHashMap());
            response.getParams().putAll(map);
        }
        return response;
    }
    public PayResponse addParams(T params) {
        this.params = params;
        return this;
    }


    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getError() {
        return StringUtils.defaultString(error);
    }

    public void setError(String error) {
        this.error = error;
    }

    public String getMessage() {
        return StringUtils.defaultString(message);
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getParams() {
        return params;
    }

    public void setParams(T params) {
        this.params = params;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }
}